package com.inetools.gwt.gajaxs.client;

import com.google.gwt.core.client.JavaScriptObject;

/**
 * Class to get input from javascript and hand it over to GCalevenderLite
 * @author ay
 *
 */
public class GCalevenderLiteJsBridge {
	// For user input -----------------------------------------------------------------------------
	// JavaScriptObject class for userId, visibility, projection, and query parameters to 
	// obtain as input from javascript
	public static class JsGCalenderDataApiInputOptions extends JavaScriptObject {
		// Overlay types always have protected, zero-arg ctors
		protected JsGCalenderDataApiInputOptions () {
		}
		
		/**
		 * Helper method to return trimmed inputStr since trim method of String class cannot be 
		 * called (like below) from inside of JSNI method:
		 * 	var nameCopy = @java.lang.String::new(Ljava/lang/String;)( name).@java.lang.String::trim()();
		 * This needs to be static method, since JavaScript class cannot be instantiated, 
		 * it seems not to allowed to call this method using "this" instance identifier like: 
		 * 	this.@com.inetools.gwetons.client.DataUnitByStyleSelector::trim(Ljava/lang/String;)( inputStr);
		 */
		public static final String trim( String inputStr) {
			return inputStr.trim();
		}
		
		protected final native String getStringStrictImpl( String name) /*-{
			if ( typeof( name) != "string") return null;
			var nameCopy = @com.inetools.gwt.gajaxs.client.GCalevenderLiteJsBridge.JsGCalenderDataApiInputOptions::trim(Ljava/lang/String;)( name);
			if ( nameCopy.length < 1) return null;
			if ( typeof( this[ nameCopy]) != "string") return null;
			var propertyStr = @com.inetools.gwt.gajaxs.client.GCalevenderLiteJsBridge.JsGCalenderDataApiInputOptions::trim(Ljava/lang/String;)( this[ nameCopy]);
			if ( propertyStr.length < 1) return null;
			return propertyStr;
		}-*/;
		
		protected final native Boolean getBooleanImpl( String name) /*-{
			if ( typeof( name) != "string") return null;
			var nameCopy = @com.inetools.gwt.gajaxs.client.GCalevenderLiteJsBridge.JsGCalenderDataApiInputOptions::trim(Ljava/lang/String;)( name);
			if ( typeof( this[ nameCopy]) != 'boolean') return null;
			return @java.lang.Boolean::valueOf(Z)( this[ nameCopy]);
		}-*/;

		protected final native Integer getIntImpl( String name) /*-{
			if ( typeof( name) != "string") return null;
			var nameCopy = @com.inetools.gwt.gajaxs.client.GCalevenderLiteJsBridge.JsGCalenderDataApiInputOptions::trim(Ljava/lang/String;)( name);
			if ( isNaN( this[ nameCopy])) return null;
			var intValue = parseInt( this[ nameCopy]);
			if ( isNaN( intValue)) return null;
			return @java.lang.Integer::valueOf(I)( intValue);
		}-*/;
		
		// Google Calendar specific query parameters ----------------------------------------------
		/**
		 * Return value could be null
		 */
		public final String getUserId() {
			return getStringStrictImpl( "userId");
		}
		
		// Refer to http://code.google.com/apis/calendar/data/2.0/reference.html#Visibility
		public final String getVisibility() {
			String visibility = getStringStrictImpl( "visibility");
			if ( visibility == null) return GCalendarEventFeedParameterDefaultValue.VISIBILITY;
			return visibility;
		};
		public final String getProjection() {
			String projection = getStringStrictImpl( "projection");
			if ( projection == null) return GCalendarEventFeedParameterDefaultValue.PROJECTION;
			return projection;
		}
		
		// Refer to http://code.google.com/apis/calendar/data/2.0/reference.html#Parameters
		/**
		 * Return value could be null
		 */
		public final String getCtz() {
			return getStringStrictImpl( "ctz");
		};
		public final Boolean getFutureEvents() {
			Boolean futureevents = getBooleanImpl( "futureevents");
			if ( futureevents == null) return GCalendarEventFeedParameterDefaultValue.FUTUREEVENTS;
			return futureevents;
		}
		public final String getOrderBy() {
			String orderby = getStringStrictImpl( "orderby");
			if ( orderby == null) return GCalendarEventFeedParameterDefaultValue.ORDERBY;
			return orderby;
		}
		/**
		 * Return value could be null
		 */
		public final String getRecurrenceExpansionStart() {
			return getStringStrictImpl( "recurrence-expansion-start");
		}
		/**
		 * Return value could be null
		 */
		public final String getRecurrenceExpansionEnd() {
			return getStringStrictImpl( "recurrence-expansion-end");
		}
		public final Boolean getSingleEvents() {
			Boolean singleevents = getBooleanImpl( "singleevents");
			if ( singleevents == null) return GCalendarEventFeedParameterDefaultValue.SINGLEEVENTS;
			return singleevents;
		};
		public final Boolean getShowHidden() {
			Boolean showhidden = getBooleanImpl( "showhidden");
			if ( showhidden == null) return GCalendarEventFeedParameterDefaultValue.SHOWHIDDEN;
			return showhidden;
		}
		public final String getSortOrder() {
			String sortorder = getStringStrictImpl( "sortorder");
			if ( sortorder == null) return GCalendarEventFeedParameterDefaultValue.SORTORDER;
			return sortorder;
		}
		/**
		 * Return value could be null
		 */
		public final String getStartMin() {
			return getStringStrictImpl( "start-min");
		}
		/**
		 * Return value could be null
		 */
		public final String getStartMax() {
			return getStringStrictImpl( "start-max");
		}
		// ----------------------------------------------------------------------------------------
		
		// Google data query parameters -----------------------------------------------------------
		/**
		 * Return value could be null
		 */
		public final String getQueryStr() {
			return getStringStrictImpl( "q");
		}
		/**
		 * Return value could be null
		 */
		public final String getAuthor() {
			return getStringStrictImpl( "author");
		}
		/**
		 * Return value could be null
		 */
		public final String getUpdatedMin() {
			return getStringStrictImpl( "updatedMin");
		}
		/**
		 * Return value could be null
		 */
		public final String getUpdatedMax() {
			return getStringStrictImpl( "updatedMax");
		}
		/**
		 * Return value could be null
		 */
		public final String getPublishedMin() {
			return getStringStrictImpl( "publishedMin");
		}
		/**
		 * Return value could be null
		 */
		public final String getPublishedMax() {
			return getStringStrictImpl( "publishedMax");
		}
		public final Integer getStartIndex() {
			Integer startIndex = getIntImpl( "startIndex");
			if ( startIndex == null) return GCalendarEventFeedParameterDefaultValue.START_INDEX;
			return startIndex; 
		}
		public final Integer getMaxResults() {
			Integer maxResults = getIntImpl( "maxResults");
			if ( maxResults == null) return GCalendarEventFeedParameterDefaultValue.MAX_RESULTS;
			return maxResults; 
		}
		// ----------------------------------------------------------------------------------------		
		
	} // protected class GCalenderDataApiInputOptions extends JavaScriptObject
	protected JsGCalenderDataApiInputOptions jsGCalenderDataApiInputOptions;
	public JsGCalenderDataApiInputOptions getJsGCalenderDataApiInputOptions() {
		return jsGCalenderDataApiInputOptions;
	}

	/**
	 * To find out whether gCalenderDataApiInputOptions is initialized
	 */
	protected boolean jsGCalenderDataApiInputOptionsInitilizationOn = false;
	public boolean isJsGCalenderDataApiInputOptionsInitilizationOn() {
		return jsGCalenderDataApiInputOptionsInitilizationOn;
	}
	
	// TODO To support multi instance, create wrapper class by extending Exportable interface, so this method on each instance can be used directly from javascript
	// About Exportable interface, refer to http://code.google.com/p/gwtchismes/wiki/Tutorial_ExportingGwtLibrariesToJavascript_en
	public final void setJsGCalenderDataApiInputOptions( JavaScriptObject nativeGCalenderDataApiInputOptions) {
		jsGCalenderDataApiInputOptions = 
			(JsGCalenderDataApiInputOptions)nativeGCalenderDataApiInputOptions;
		jsGCalenderDataApiInputOptionsInitilizationOn = true;
	}
	/**
	 * Expose setGCalenderDataApiInputOptions method (which set gCalenderDataApiInputOptions field) to javascript
	 */
	public native final void exposeSetGCalenderDataApiInputOptionsMethodToJs( GCalevenderLiteJsBridge thisInstance) /*-{
		$wnd.setGCalenderDataApiInputOptions = function( gCalenderDataApiInputOptions) {
			thisInstance.@com.inetools.gwt.gajaxs.client.GCalevenderLiteJsBridge::setJsGCalenderDataApiInputOptions(Lcom/google/gwt/core/client/JavaScriptObject;)( gCalenderDataApiInputOptions);
		}
	}-*/;
	
	// --------------------------------------------------------------------------------------------
}
